#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2019/2/18 15:01
@desc
"""

import time
from concurrent.futures import ThreadPoolExecutor
import threading


def func(n):
    time.sleep(0.5)
    # print(n)
    return n, n * n


def call_back(m):
    # print(m.__dict__)
    res = m.result()
    print('%s 结果是 %s -> %s' % (threading.currentThread().getName(), res[0], res[1]))
    # print('%s, 结果是 %s' % (m.name(), m.result()))


tpool = ThreadPoolExecutor(max_workers=5, thread_name_prefix='sub-task-')  # 默认 不要超过cpu个数*5
# print(tpool.__dict__)
for i in range(20):
    tpool.submit(func, i).add_done_callback(call_back)

# tpool.map(func, range(20))  # 拿不到返回值
# t_lst = []
# for i in range(20):
#     t = tpool.submit(func, i)
#     t_lst.append(t)
# # tpool.shutdown()  # close+join    #
# print('主线程')
# for t in t_lst:
#     print('***', t.result())
# call_back(t)
# ftp
# 并发编程
